﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GuiaVinum.Model;

namespace GuiaVinum.Repository
{
    public class GuiaVinumDb : DbContext, IDisposedTracker
    {
        public GuiaVinumDb():base("DefaultConnection")
        {
                
        }

        public DbSet<UserProfile> UserProfiles { get; set; }

        public DbSet<Vino> Vinos { get; set; }
        public DbSet<ExperienciaUsuario> ExperienciasUsuario { get; set; }
        public DbSet<Pais> Paises { get; set; }
        public DbSet<Region> Regiones { get; set; }
        public DbSet<CategoriaVarietal> CategoriasVarietal { get; set; }
        public DbSet<Varietal> Varietales { get; set; }
        public DbSet<Bodega> Bodegas { get; set; }
        public DbSet<Cosecha> Cosechas { get; set; }

        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public bool IsDisposed { get; set; }

        protected override void Dispose(bool disposing)
        {
            
            IsDisposed = true;
            base.Dispose(disposing);
        }



        internal bool TableExists(string tableName)
        {
            return this.Database.SqlQuery<int?>(string.Format(@"
                         SELECT 1 FROM sys.tables AS T
                         INNER JOIN sys.schemas AS S ON T.schema_id = S.schema_id
                         WHERE S.Name = 'dbo' AND T.Name = '{0}'",tableName))
                                 .SingleOrDefault() != null;
        }
    }
}
